package cn.humaohua.study.hadoop.cmdrunner.c5;

import cn.humaohua.study.hadoop.cmdrunner.Test;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.RawComparator;
import org.apache.hadoop.io.WritableComparator;
import org.springframework.stereotype.Component;

@Component
public class ComparableTest implements Test {
    IntWritable one = new IntWritable(1);
    IntWritable two = new IntWritable(2);

    @Override
    public void test() throws Exception {
        //测试对象比较
        int times = 1000000000;
        long s = System.currentTimeMillis();
        for (int i = 0; i < times; i++) {
            one.compareTo(two);
        }
        long e = System.currentTimeMillis();
        System.out.println(String.format("times=%d,cost=%d", times, (e - s)));


        //基本类型比较
        RawComparator<IntWritable> comparator = WritableComparator.get(IntWritable.class);
        s = System.currentTimeMillis();
        for (int i = 0; i < times; i++) {
            comparator.compare(one, two);
        }
        e = System.currentTimeMillis();
        System.out.println(String.format("times=%d,cost=%d", times, (e - s)));

//        times=1000000000,cost=27
//        times=1000000000,cost=4
    }
}
